package org.jeecg.modules.fk.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.MyStringUtil;
import org.jeecg.modules.fk.entity.BusinessDomain;
import org.jeecg.modules.fk.entity.FactorPool;
import org.jeecg.modules.fk.service.IFactorPoolService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: factor_pool
 * @Author: ZJJ
 * @Date: 2021-04-30
 * @Version: V1.0
 */
@Api(tags = "因子池")
@RestController
@RequestMapping("/factor_pool/factorPool")
@Slf4j
public class FactorPoolController extends JeecgController<FactorPool, IFactorPoolService> {
    @Autowired
    private IFactorPoolService factorPoolService;

    /**
     * 分页列表查询
     *
     * @param factorPool
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "factor_pool-分页列表查询")
    @ApiOperation(value = "factor_pool-分页列表查询", notes = "factor_pool-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(FactorPool factorPool,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        //LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        /*QueryWrapper<FactorPool> queryWrapper = QueryGenerator.initQueryWrapper(factorPool, req.getParameterMap());
        Page<FactorPool> page = new Page<FactorPool>(pageNo, pageSize);*/
        List<FactorPool> list = factorPoolService.queryPageList(factorPool);
        Page<FactorPool> page = new Page<>(pageNo, pageSize);
        PageInfo<FactorPool> pageInfo = new PageInfo<>(list);
        page.setRecords(pageInfo.getList());
        page.setCurrent(pageNo);
        page.setTotal(pageInfo.getTotal());
        page.setSize(pageSize);
        Result<Object> result = new Result<>();
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }

    /**
     * 添加
     *
     * @param factorPool
     * @return
     */
    @AutoLog(value = "factor_pool-添加")
    @ApiOperation(value = "factor_pool-添加", notes = "factor_pool-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FactorPool factorPool) {
        factorPoolService.save(factorPool);

        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param factorPool
     * @return
     */
    @AutoLog(value = "factor_pool-编辑")
    @ApiOperation(value = "factor_pool-编辑", notes = "factor_pool-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FactorPool factorPool) {
        factorPoolService.updateById(factorPool);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "factor_pool-通过id删除")
    @ApiOperation(value = "factor_pool-通过id删除", notes = "factor_pool-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        factorPoolService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "factor_pool-批量删除")
    @ApiOperation(value = "factor_pool-批量删除", notes = "factor_pool-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.factorPoolService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "factor_pool-通过id查询")
    @ApiOperation(value = "factor_pool-通过id查询", notes = "factor_pool-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        FactorPool factorPool = factorPoolService.getById(id);
        if (factorPool == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(factorPool);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param factorPool
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FactorPool factorPool) {
        return super.exportXls(request, factorPool, FactorPool.class, "factor_pool");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FactorPool.class);
    }

}
